/**
 * 物流状态管理
 */
var logisticStatusGrid = function(orderId) {
	// 定义表的每列（对应表头信息）
	var logisticStatusCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				dataIndex : "id",
				header : "编号",
				sortable : true,
				width : 70
			}, {
				dataIndex : "orderId",
				header : "订单编号",
				sortable : true,
				width : 70
			}, {
				dataIndex : "isShow",
				header : "是否显示",
				sortable : true,
				width : 80,
				renderer : function(value) {
					return PmsUtil.IsDafault(value);
				}
			}, {
				dataIndex : "statusName",
				header : "物流信息",
				sortable : true,
				width : 200,
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="收货人地址:" ext:qtip="' + value
								+ '"';
						return value;
					}
				}
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "createdName",
				header : "操作人",
				sortable : true,
				width : 60
			}, {
				header : "管理",
				width : 100,
				dataIndex : "id",
				renderer : function(j, i, g, m, h) {
					var logisticId = g.data.id;
					var logisticMessage = g.data.statusName;
					var k = "";
					if (isGranted("logisticStatus_物流状态修改")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="updateDeliveryInfoWindows('
								+ logisticId
								+ ",'"
								+ logisticMessage
								+ "')\">修改</a>";
					}
					return k;
				}
			}]);

	// 定义分析器
	var logisticStatusReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "orderId"
						}, {
							name : "statusName"
						}, {
							name : "isShow"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}]
			});

	// 取数据store
	var logisticStatusViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath
									+ "/orderManager/queryLogisticStatus"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 25,
					orderId : orderId
				},
				reader : logisticStatusReader
			});
	// 定义grid的toolbar
	var logisticStatusTb = new Ext.Toolbar({
				height : 25,
				items : []
			});
	logisticStatusTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : 'logisticStatus_刷新',
				pressed : true,
				handler : function() {
					queryLogisticStatus();
				}
			}));
	var logisticStatusGrid = new Ext.grid.GridPanel({
				id : 'logisticStatusGrid',
				store : logisticStatusViewStore,
				cm : logisticStatusCm,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 100%',
				frame : true,
				tbar : logisticStatusTb,
				bbar : new PG.PagingBar({
							id : 'logisticStatusGridPage',
							store : logisticStatusViewStore
						})
			});
	return logisticStatusGrid;
};

/**
 * 修改配送信息
 * 
 * @param {}
 *            orderId
 * @return {}
 */
function updateDeliveryInfoWindows(logisticId, logisticMessage) {
	var updateOrderDeliveryInfoForm = new Ext.FormPanel({
				frame : true,
				bodyBorder : false,
				viewConfig : {
					forceFit : true
				},
				defaultType : "textfield",
				items : [{
							name : "logisticId",
							xtype : "hidden",
							value : logisticId
						}, {
							fieldLabel : "物流信息",
							xtype : "textarea",
							name : "logisticMessage",
							allowBlank : false,
							value : logisticMessage,
							width : 200,
							height : 100,
							vtype : "validateMaxLength",
							confirmTo : 400
						}]
			});
	var orderDeliveryInfoWindow = new Ext.Window({
		title : "修改物流信息",
		width : 350,
		height : 200,
		closable : true,
		modal : true,
		layout : "fit",
		autoDestroy : true,
		draggable : false, // 不可拖动
		resizable : false, // 不可调整
		plain : true,
		buttonAlign : "center",
		items : updateOrderDeliveryInfoForm,
		buttons : [{
			text : "保存",
			width : 60,
			handler : function() {
				var uploadForm = updateOrderDeliveryInfoForm.getForm();
				if (uploadForm.isValid()) {
					Ext.Msg.confirm("信息确认", "确认物流信息吗？", function(c) {
						if (c == "yes") {
							uploadForm.submit({
								url : __ctxPath
										+ "/orderManager/updateOrderLogisticsMessage",
								method : "post",
								waitTitle : "请稍后",
								waitMsg : "正在提交数据...",
								success : function(response, options) {
									var jsonData = Ext.util.JSON
											.decode(options.response.responseText);
									Ext.MessageBox.show({
												title : "信息提示",
												msg : jsonData.data,
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.INFO
											});
									Ext.getCmp("logisticStatusGrid").getStore()
											.reload();
									orderDeliveryInfoWindow.close();
								},
								failure : function(response, options) {
									var jsonData = Ext.util.JSON
											.decode(options.response.responseText);
									Ext.MessageBox.show({
												title : "操作信息",
												msg : jsonData.data,
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.ERROR
											});
								}
							});
						}
					})
				}
			}
		}, {
			text : "重置",
			width : 60,
			handler : function() {
				updateOrderDeliveryInfoForm.form.reset();
			}
		}, {
			text : "取消",
			width : 60,
			handler : function() {
				orderDeliveryInfoWindow.close();
			}
		}]
	}).show();
	return orderDeliveryInfoWindow.show();
};

// 查询
function queryLogisticStatus() {
	var grid = Ext.getCmp("logisticStatusGrid");
	var store = grid.store;
	var pagesize = Ext.getCmp('logisticStatusGridPage').pageSize;
	store.load({
				params : {
					start : 0,
					limit : pagesize
				}
			});
}
